package com.hq.ims.data.utils;


import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * 获取stream相关工具类
 */
@Component
public class StreamUtils {

    /**
     * 根据集合的属性进行去重
     */
    public static <T> List<T> distinctListByProperty(List<T> filteredList, Function<? super T, ?> inputProperty) {
        return filteredList.stream().filter(distinctJudgmentPredicate(inputProperty)).collect(Collectors.toList());
    }


    /**
     * stream的去重属性判断
     */
    private static <T> Predicate<T> distinctJudgmentPredicate(Function<? super T, ?> object) {
        //去重的map集合
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return t -> seen.putIfAbsent(object.apply(t), Boolean.TRUE) == null;
    }
}
